package com.hymall.service.admin;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hymall.bean.MarketTopic;
import com.hymall.bean.MarketTopicExample;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.BaseRespVo;
import com.hymall.bean.common.CommonData;
import com.hymall.mapper.MarketTopicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @author 赵思源
 * @since 2022/07/19 10:19 接口实现
 */
@Service
public class AdminTopicServiceImpl implements AdminTopicService {

    @Autowired
    MarketTopicMapper marketTopicMapper;

    /**
     * 显示推广信息列表
     * @author 赵思源
     * @date 2022/7/19 11:17
     * 按价格排序
     *http://182.92.235.201:8083/admin/topic/list?page=1&limit=20&title=&sort=price&order=desc
     */
    @Override
    public CommonData queryTopicList(String title,String subtitle, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit(),info.getSort());

        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        if(title != null && !"".equals(title)){
            title = "%" + title + "%";
            criteria.andTitleLike(title);
        }
        if(subtitle != null && !"".equals(subtitle)){
            subtitle = "%" + subtitle + "%";
            criteria.andSubtitleLike(subtitle);
        }
        criteria.andDeletedEqualTo(false);

        // order by add_time desc
        marketTopicExample.setOrderByClause(info.getSort()+" "+info.getOrder());
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExampleWithBLOBs(marketTopicExample);

        /*
        if (sort.equals("id")){
            marketTopics.sort(Comparator.comparingInt(MarketTopic::getId));
        } else if (sort.equals("price")){
            marketTopics.sort(new Comparator<MarketTopic>() {
                @Override
                public int compare(MarketTopic o1, MarketTopic o2) {
                   return o1.getPrice().compareTo(o2.getPrice());
                }
            });
            // 默认按时间排序 sort.equals("add_time")
        } else {
            marketTopics.sort(Comparator.comparing(MarketTopic::getAddTime));
        }
        */
        PageInfo<MarketTopic> pageInfo = new PageInfo<>(marketTopics);
        return CommonData.data(pageInfo);

    }

    /**
     * 创建一个推广活动
     * @author Zhaosiyuan
     * @date 2022/7/19 20:11
     *
     */
    @Override
    @Transactional
    public BaseRespVo creat(MarketTopic topic) {
        //
        Date date = new Date();
        if(topic.getAddTime() == null){
            topic.setAddTime(date);
        }
        topic.setUpdateTime(date);
        int i = marketTopicMapper.insertSelective(topic);
        if (i == 1) {
            return BaseRespVo.ok(topic);
        }
        return BaseRespVo.error(507,"商品或主题重复，请重试");
    }

    /**
     * 删除专题活动
     * @author Zhaosiyuan
     * @date 2022/7/20  10:22
     *
     */
    @Override
    public int delete(MarketTopic topic) {
        Integer id = topic.getId();
        marketTopicMapper.deleteByPrimaryKey(id);
        return 200;
    }

    @Override
    public int batchDelete(Integer[] ids) {
        for (Integer id : ids) {
            marketTopicMapper.deleteByPrimaryKey(id);
        }
        return 200;
    }

    @Override
    public MarketTopic selectTopicById(Integer id) {

        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        return marketTopic;
    }

    @Override
    public BaseRespVo updateTopic(MarketTopic topic) {
        int i = marketTopicMapper.updateByPrimaryKey(topic);
        if (i == 1) {
            return BaseRespVo.ok(topic);
        }
        return BaseRespVo.error(507,"更新失败，请重试");
    }

}
